//
//  AddViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/13.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "AddViewController.h"
#import "AddView.h"
#import "Base.h"
#import "SecondAddView.h"
#import "BuMen.h"
#import "ReadyDiSignle.h"
#import "School.h"
@interface AddViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>
{
    UIView *photoView;
    UIScrollView *_mainScroll;
    
    AddView *_itemview;
    SecondAddView *_buttonView;
    UITableView *mainTable;
    int _type;
    
    NSString *name;       // 姓名昵称
    NSString *gender;//性别
    NSString *dptId;      // 所属部门ID
    NSString *dptName;    // 所属部门名
    NSNumber *type; 
    NSString *tel;        // 联系电话
    NSNumber *role;       // 角色
    NSString *mktId;      // 仓库Id
    NSString *mktName;    // 仓库名
    NSString *_id;         // 身份证号
}
@end

@implementation AddViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setBack];
    self.view.backgroundColor=COLOR;
    [self addallvies];
   
}
-(void)addallvies
{
    _mainScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _mainScroll.backgroundColor=COLOR;
    
    [self.view addSubview:_mainScroll];
    
    photoView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    photoView.backgroundColor=[UIColor whiteColor];
    UIImageView *imagep=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    imagep.layer.cornerRadius=40;
    imagep.backgroundColor=COLOR;
    [photoView addSubview:imagep];
    [_mainScroll addSubview:photoView];
    NSArray *array=@[@"员工姓名:",@"联系电话:",@"身份证号:"];
    for (int i=0; i<3; i++) 
        {
        _itemview=[[AddView alloc] initWithFrame:CGRectMake(0, 105+40*i, WIDTH, 40)];
        
        _itemview.nameLaber.text=array[i];
        _itemview.textField.delegate=self;

        _itemview.textField.tag=200+i;
        if (self.usermodel!=nil)
            {
            NSArray *array=@[self.usermodel.name,self.usermodel.tel,self.usermodel.id];
            
            _itemview.textField.text=array[i];
            }
         else
             {
             
             }
        [_mainScroll addSubview:_itemview];
        }
    NSArray *array2=@[@"员工性别:",@"所在部门:",@"员工类型:",@"系统权限:",@"所在仓库:"];
    for (int i=0; i<5; i++)
        {
        _buttonView=[[SecondAddView alloc] initWithFrame:CGRectMake(0, 225+40*i, WIDTH, 40)];
        _buttonView.nameLaber.text=array2[i];
        [_buttonView.button setTag:110+i];
        [_buttonView.button addTarget:self action:@selector(shitAction:) forControlEvents:UIControlEventTouchUpInside];
        [_mainScroll addSubview:_buttonView];
        }
    UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_buttonView.frame)+10, WIDTH-20, 40)];
    button.layer.cornerRadius=4;
    button.backgroundColor=BLUECOLOL;
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [_mainScroll addSubview:button];
    _mainScroll.contentSize=CGSizeMake(0, CGRectGetMaxY(button.frame)+10);
   }
-(void)shitAction:(UIButton *)sender
{
    if (sender.tag==110)
        {
        _type=1;
               }
    else if(sender.tag==111)
        {
        [[ReadyDiSignle signle] bumenliebiao];
        
        [[ReadyDiSignle signle] addObserver:self forKeyPath:@"bumenArray" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
        _type=2;
        }
    else if(sender.tag==112)
        {
        _type=3;
        }
    else if(sender.tag==113)
        {
        _type=4;
        }
    else if(sender.tag==114)
        {
        _type=5;
        [[ReadyDiSignle signle] allSchool];
        [[ReadyDiSignle signle] addObserver:self forKeyPath:@"schoolArray" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
        }
    [self addtableview];
    [mainTable reloadData];

}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"bumenArray"]) 
        {
        [mainTable reloadData];
        
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"bumenArray"];
        }
    else if ([keyPath isEqualToString:@"schoolArray"]) 
        {
        [mainTable reloadData];
        
        [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"schoolArray"];
        }

}

-(void)addtableview
{   [mainTable removeFromSuperview];
    if (_type==1) 
        {
        mainTable=[[UITableView alloc] initWithFrame:CGRectMake(WIDTH/2-40, HEIGHT/2, 80, 88)];
        mainTable.layer.cornerRadius=4;
        }
    if (_type==2) 
        {
        mainTable=[[UITableView alloc] initWithFrame:CGRectMake(WIDTH/2-50, HEIGHT/2, 100, 166)];
         mainTable.layer.cornerRadius=4;
        }
    if (_type==3) 
        {
        mainTable=[[UITableView alloc] initWithFrame:CGRectMake(WIDTH/2-50, HEIGHT/2, 100, 166)];
        mainTable.layer.cornerRadius=4;
        }
    if (_type==4) 
        {
        mainTable=[[UITableView alloc] initWithFrame:CGRectMake(WIDTH/2-80, HEIGHT/2, 160, 166)];
        mainTable.layer.cornerRadius=4;
        }
    if (_type==5) 
        {
        mainTable=[[UITableView alloc] initWithFrame:CGRectMake(50, HEIGHT/2, WIDTH-100, 166)];
        mainTable.layer.cornerRadius=4;
        }
    
    mainTable.delegate=self;
    mainTable.dataSource=self;
    mainTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:mainTable];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_type==1)
        {
        return 2;
       }
    else if(_type==2)
        {
        return [ReadyDiSignle signle].bumenArray.count;
        }
    else if(_type==3)
        {
        return 4;
        }
    else if(_type==4)
        {
        return 5;
        }
    else if(_type==5)
        {
        return [ReadyDiSignle signle].schoolArray.count;
        }
    else
        {
        return 0;
        }
   
}

  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid=@"addcellid";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil)
        {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
    if (_type==1) 
        {
        NSArray *array=@[@"男",@"女"];
        cell.textLabel.text=array[indexPath.row];
        }
    else if(_type==2)
        {
        BuMen *bumen=[ReadyDiSignle signle].bumenArray[indexPath.row];
        cell.textLabel.text=bumen.name;
        }
    else if(_type==3)
        {
        NSArray *array=@[@"全职",@"实习",@"使用",@"兼职"];
        cell.textLabel.text=array[indexPath.row];
        }
    else if(_type==4)
        {
        NSArray *array=@[@"送货员",@"配货员",@"配送员",@"配送管理员",@"超级管理员"];
        cell.textLabel.text=array[indexPath.row];
        }
    else if(_type==5)
        {
        School *school=[ReadyDiSignle signle].schoolArray[indexPath.row];
        cell.textLabel.text=school.name;

        }
   
    UIView *view=[[UIView alloc] initWithFrame:cell.frame];
    view.backgroundColor=[UIColor redColor];
    cell.backgroundColor=COLOR;
    cell.textLabel.textColor=[UIColor grayColor];
    cell.selectedBackgroundView=view;
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(0, 42, cell.frame.size.width, 2)];
    l.backgroundColor=COLOR;
    [cell addSubview:l];
    return cell;
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
     
    if (_type==1) 
        { 
        NSArray *array=@[@"男",@"女"];
        UIButton *button=(UIButton *)[self.view viewWithTag:110];
        if(indexPath.row==0)
            {
            gender=@"m";
            }
         else
             {
             gender=@"f";
             }
        [button setTitle:array[indexPath.row] forState:UIControlStateNormal];
           
        }
    else if(_type==2)
        {
        BuMen *bumen=[ReadyDiSignle signle].bumenArray[indexPath.row];
       
        UIButton *button=(UIButton *)[self.view viewWithTag:111];
        
        dptName=bumen.name;
        dptId=bumen._id;
        
        [button setTitle:bumen.name forState:UIControlStateNormal];
       
        }

    else if(_type==3)
        {
        NSArray *array=@[@"全职",@"实习",@"试用",@"兼职"];
        UIButton *button=(UIButton *)[self.view viewWithTag:112];
        if (indexPath.row==0)
            {
            type=[NSNumber numberWithInt:1];
            }
        else if(indexPath.row==1)
            {
             type=[NSNumber numberWithInt:3];
            }
        else if(indexPath.row==2)
            {
            type=[NSNumber numberWithInt:4];
            }
        else if(indexPath.row==3)
            {
            type=[NSNumber numberWithInt:2];
            }
        [button setTitle:array[indexPath.row] forState:UIControlStateNormal];
         
        }
    else if(_type==4)
        {
        NSArray *array=@[@"送货员",@"配货员",@"配送员",@"配送管理员",@"超级管理员"];
        
        UIButton *button=(UIButton *)[self.view viewWithTag:113];
        if (indexPath.row==0)
            {
            role=[NSNumber numberWithInt:3];
            }
        else if(indexPath.row==1)
            {
            role=[NSNumber numberWithInt:4];
            }
        else if(indexPath.row==2)
            {
            role=[NSNumber numberWithInt:5];
            }
        else if(indexPath.row==3)
            {
            role=[NSNumber numberWithInt:2];
            }
        else if(indexPath.row==4)
            {
            role=[NSNumber numberWithInt:1];
            }


        [button setTitle:array[indexPath.row] forState:UIControlStateNormal];
        
        }
    else if(_type==5)
        {
       School *school=[ReadyDiSignle signle].schoolArray[indexPath.row];
        
        UIButton *button=(UIButton *)[self.view viewWithTag:114];
        
        mktId=school.mktId;
        mktName=school.name;
        
        [button setTitle:school.name forState:UIControlStateNormal];
        
        }

    
    [mainTable removeFromSuperview];
}
-(void)addAction:(UIButton *)sender
{
    UITextField *namelaber=(UITextField *)[self.view viewWithTag:200];
    name=namelaber.text;
    UITextField *phonelaber=(UITextField *)[self.view viewWithTag:201];
    tel=phonelaber.text;
     UITextField *idlaber=(UITextField *)[self.view viewWithTag:202];
    _id=idlaber.text;
        // NSLog(@"%@ %@ %@",name,tel,_id);
    if (name.length<1)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"输入名字" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
       }
    else if(tel.length<11)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"输入正确的电话" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }
    else if(_id.length<18)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"输入正确的身份证号码" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }
    else if(gender==nil)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"选择性别" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }
    else if(dptId==nil)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"选择部门" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }

    else if(type==nil)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"选择类型" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }

    else if(role==nil)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"选择权限" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }

    else if(mktId==nil)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"选择仓库" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
        [a show];
        }
    else
        {
       
        
            // NSString *ss=[NSString stringWithFormat:@"name=%@&gender=%@&dptId=%@&ID=%@&tel=%@&role=%@&mktId=%@&type=%@",name,gender,dptId,_id,tel,role,mktId,type];
        
        NSArray *keys=@[@"name",@"gender",@"dptId",@"id",@"tel",@"role",@"mktId",@"type"];        
        NSArray *values=@[name,gender,dptId,_id,tel,role,mktId,type];
        
        NSDictionary *dict=[NSDictionary dictionaryWithObjects:values forKeys:keys];
            //转化成json格式
      
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            // NSLog(@"%@", jsonString);
       
            //[[ReadyDiSignle signle] addWorks:dict];
        
        if (self.usermodel!=nil)
            {
            NSArray *keys=@[@"dptId",@"tel",@"role",@"mktId",@"type"];        
            NSArray *values=@[dptId,tel,role,mktId,type];
            
            NSDictionary *changdict=[NSDictionary dictionaryWithObjects:values forKeys:keys];

            [ReadyDiSignle signle].oidString= self.usermodel.account;
            [[ReadyDiSignle signle] changeWork:changdict]; 
           
           }
        else
            {
        [[ReadyDiSignle signle] addWork:dict];
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            
            
            
            [nc addObserver:self
                   selector:@selector(handleColorChange)
                       name:@"do"
                     object:nil];
            }
        }
}
-(void)handleColorChange{
    UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"添加成功" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
    [a show];
 }
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
